package com.sefonsoft.dataanalysis.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 汉字的拼音处理
 * @author xgj
 * @Date2017年9月26日t上午11:06:57
 */
public class PinyinUtils {

	 /**
     * 获得汉语拼音首字母
     *
     * @param chines 汉字
     * @return
     */
    public static String getAlpha(String chines) {
        String pinyinName = "";
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(
                            nameChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName += nameChar[i];
            }
        }
        return pinyinName;
    }

    /**
     * 将字符串中的中文转化为拼音,英文字符不变
     *
     * @param inputString 汉字
     * @return
     */
    public static String getPingYin(String inputString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        String output = "";
        if (inputString != null && inputString.length() > 0
                && !"null".equals(inputString)) {
            char[] input = inputString.trim().toCharArray();
            try {
                for (int i = 0; i < input.length; i++) {
                    if (java.lang.Character.toString(input[i]).matches(
                            "[\\u4E00-\\u9FA5]+")) {
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                                input[i], format);
                        output += temp[0];
                    } else
                        output += java.lang.Character.toString(input[i]);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            return "*";
        }
        return output;
    }
    
    /**
     * 将字符中的中文转化为拼音
     *
     * @param c 字符
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination 
     */
    public static String getPingYin(char c) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        String output = "";
        if (java.lang.Character.toString(c).matches(
                "[\\u4E00-\\u9FA5]+")) {
			try {
				String[] temp = PinyinHelper.toHanyuPinyinStringArray(
				        c, format);
				output = temp[0];
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				return java.lang.Character.toString(c);
			}
        } else {
            output = java.lang.Character.toString(c);
    	}
        return output;
    }

    /**
     * 汉字转换位汉语拼音首字母，英文字符不变
     *
     * @param chines 汉字
     * @return 拼音
     */
    public static String converterToFirstSpell(String chines) {
        String pinyinName = "";
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(
                            nameChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName += nameChar[i];
            }
        }
        return pinyinName;
    }
    
    /**
     * 判断字符串首字母是否是汉字
     *
     * @param str
     * @return
     */
    public static boolean isHanZi (Object obj) {
    	if (obj == null) {
    		return false;
    	}
    	String str = obj.toString();
    	if (str.length()>1) {
    		return Character.toString(str.charAt(0)).matches("[\\u4E00-\\u9FA5]+");
    	} else {
    		return str.matches("[\\u4E00-\\u9FA5]+");
    	}
    }
    
    /**
     *判断字符是否是汉字
     * @return
     */
    public static boolean isHanZiChar (Character c) {
    	String str = Character.toString(c);
		return str.matches("[\\u4E00-\\u9FA5]+");
    }
    
    /**
     * 判断字符串是否全是汉字
     *
     * @param str
     * @return
     */
    public static boolean isRealHanZi (Object obj) {
    	if (obj == null) {
    		return false;
    	}
    	String str = obj.toString();
    	for (int i = 0; i < str.length(); i++) {
    		if (Character.toString(str.charAt(i)).matches("[\\u4E00-\\u9FA5]+")) {
    			return false;
    		}
		}
    	return true;
    }
    
    
    /**
     * 判断字符串是否包含汉字
     *
     * @param str
     * @return
     */
    public static boolean containHanZi (Object obj) {
    	if (obj == null) {
    		return false;
    	}
    	String str = obj.toString();
    	for (int i = 0; i < str.length(); i++) {
    		if (Character.toString(str.charAt(i)).matches("[\\u4E00-\\u9FA5]+")) {
    			return true;
    		}
		}
    	return false;
    }
    /**
     * 汉字比较大小
     * @param str
     * @param str
     * @return
     */
    public static int compare(String one, String two) {
    	if (one == null && two == null) {
    		return 0;
    	} 
    	if (one == null || two == null){
    		return one == null ? 1 : -1;
    	}
    	String str1 = getPingYin(one);
    	String str2 = getPingYin(two);
    	return str1.compareTo(str2);
    }
}
